import axios from 'axios'
import {runConfig} from '../config'

let cancel = {}
let promiseArr = {}
const CancelToken = axios.CancelToken

axios.interceptors.request.use(config => {
  if (promiseArr[config.url]) {
    promiseArr[config.url]('重複操作導致操作取消')
    promiseArr[config.url] = cancel
  } else {
    promiseArr[config.url] = cancel
  }
  return config
}, error => {
  return Promise.reject(error)
})

axios.interceptors.response.use(response => {
  return response
}, err => {
  if (err && err.message && err.message === '重複操作導致操作取消') {
    return Promise.resolve({
      data: {
        code: -408,
        message: '操作過於頻繁，請稍後重試！',
        head: {code: -408, errcode: -408, message: '操作過於頻繁，請稍後重試！'}
      }
    })
  }
  if (err && err.response) {
    switch (err.response.status) {
      case 400:
        err.message = '錯誤請求'
        break
      case 401:
        err.message = '未授權，請重新登錄'
        break
      case 403:
        err.message = '拒絕訪問'
        break
      case 404:
        err.message = '請求錯誤,未找到該資源'
        break
      case 405:
        err.message = '請求方法未允許'
        break
      case 408:
        err.message = '請求超時'
        break
      case 500:
        err.message = '服務器端出錯'
        break
      case 501:
        err.message = '網絡未實現'
        break
      case 502:
        err.message = '網絡錯誤'
        break
      case 503:
        err.message = '服務不可用'
        break
      case 504:
        err.message = '網絡超時'
        break
      case 505:
        err.message = 'http版本不支持該請求'
        break
      case -408:
        err.message = '操作取消'
        break
      default:
        err.message = '連接錯誤'
    }
  } else {
    err.message = '連接到服務器失敗'
  }
  if ([undefined, null].includes(err.response)) {
    return Promise.resolve({
      data: {
        code: -1,
        message: err.message,
        head: {code: -1, errcode: -1, message: err.message}
      }
    })
  } else {
    return Promise.resolve({
      data: {
        code: err.response.status,
        message: err.message + ' ' + err.response.status,
        head: {
          code: err.response.status,
          errcode: err.response.status,
          message: err.message + ' ' + err.response.status
        }
      }
    })
  }
})

axios.defaults.baseURL = runConfig.baseApi
// 設置默認請求頭
axios.defaults.headers = {
  'X-Requested-With': 'XMLHttpRequest'
}
axios.defaults.timeout = 60 * 1000

export default {
  // get請求
  get: function (url, param) {
    return new Promise((resolve) => {
      axios({
        method: 'get',
        url,
        params: param,
        cancelToken: new CancelToken(c => {
          cancel = c
        })
      }).then(res => {
        resolve(res.data)
      })
    })
  },
  // post請求
  post: function (url, param) {
    return new Promise((resolve) => {
      axios({
        headers: {
          token: "11",
          'Content-Type': 'application/json'
        },
        xhrFields: {
          withCredentials: true
        },
        method: 'post',
        url,
        data: param,
        cancelToken: new CancelToken(c => {
          cancel = c
        })
      }).then(res => {
        resolve(res.data)
      })
    })
  }
}
